package com.lzh.persevere.manager;


import com.lzh.persevere.utils.ByteUtils;

import java.io.Serializable;
import java.net.InetAddress;

public class UUIDHexGenerator {
    private static final int ip;
    private static short counter;
    private static final int jvm;
    private final String sep;

    public UUIDHexGenerator() {
        this.sep = "";
    }

    public UUIDHexGenerator(String sep) {
        this.sep = sep;
    }

    protected String format(int intval) {
        String formatted = Integer.toHexString(intval);
        StringBuilder buf = new StringBuilder("00000000");
        buf.replace(8 - formatted.length(), 8, formatted);
        return buf.toString();
    }

    protected String format(short shortval) {
        String formatted = Integer.toHexString(shortval);
        StringBuilder buf = new StringBuilder("0000");
        buf.replace(4 - formatted.length(), 4, formatted);
        return buf.toString();
    }

    public Serializable generate() {
        return (new StringBuilder(36)).append(this.format(this.getIP())).append(this.sep).append(this.format(this.getJVM())).append(this.sep).append(this.format(this.getHiTime())).append(this.sep).append(this.format(this.getLoTime())).append(this.sep).append(this.format(this.getCount())).toString();
    }

    private int getJVM() {
        return jvm;
    }

    private short getCount() {
        synchronized(UUIDHexGenerator.class) {
            if (counter < 0) {
                counter = 0;
            }

            short var10000 = counter;
            counter = (short)(var10000 + 1);
            return var10000;
        }
    }

    private int getIP() {
        return ip;
    }

    private short getHiTime() {
        return (short)((int)(System.currentTimeMillis() >>> 32));
    }

    private int getLoTime() {
        return (int)System.currentTimeMillis();
    }

    static {
        int ipadd;
        try {
            ipadd = ByteUtils.toInt(InetAddress.getLocalHost().getAddress());
        } catch (Exception var2) {
            ipadd = 0;
        }

        ip = ipadd;
        counter = 0;
        jvm = (int)(System.currentTimeMillis() >>> 8);
    }
}
